< previous page page_248 next page >

Page 248
e10a6a0a59f035e870d90cca05b85fce.gif
                a_d = f / M(i) / d
                Vx(i) = Vx(i)  a_d * dx
                Vy(i) = Vy(i)  a_d * dy

                a_d = f / M(j) / d
                Vx(j) = Vx(j) + a_d * dx
                Vy(j) = Vy(j) + a_d * dy
            Next j
        Next i

        ' Move all the planets.
        For i = 1 To NumPlanets
            Cx(i) = Cx(i) + Vx(i)
            Cy(i) = Cy(i) + Vy(i)
        Next i

        ' Restore the background.
        SetBitmapBits picCanvas.Image, _
            BitmapNumBytes, Bytes(1, 1)

        ' Redraw the planets.
        For i = 1 To NumPlanets
            picCanvas.FillColor = Clr(i)
            picCanvas.Circle (Cx(i), Cy(i)), R(i), Clr(i)
        Next i

        ' Wait until it's time for the next frame.
        next_time = next_time + ms_per_frame
        WaitTill next_time
    Loop
End Sub
Example program Cannon uses gravitational rules to simulate cannon fire. Enter the cannon's angle and the projectile's initial speed. At each step in the animation, the program subtracts a downward acceleration due to gravity from the projectile's vertical velocity component.
Using other physical laws, you can build other animation programs that simulate colliding balls, springs, objects in magnetic fields, objects in flight, and so forth.
Scripts
Simulation programs such as the Bounce programs, Planets, and Cannon use physical laws to determine where objects should be drawn. Given information about the simulation, the program uses the laws to decide how the objects move.
A script explicitly lists the locations of the objects for different frames in the animation. A script must describe everything the program needs to know to display objects properly. That might include the position, orientation, shape, and color of the objects being displayed.

 
< previous page page_248 next page >